<template>
  <div>
    <el-card class="el-card" shadow="hover" :body-style="{ padding: '0px' }">
      <!-- 添加数码类别弹出框 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <span class="inputName">(类型名称)</span>
        <el-form
          :model="ruleForm"
          :hide-required-asterisk="true"
          :status-icon="true"
          :rules="rules"
          ref="ruleForm"
        >
          <el-form-item
            label="品牌名称"
            prop="name"
            :label-width="formLabelWidth"
          >
            <el-input class="inp1" v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item
            label="品牌路径"
            prop="road"
            :label-width="formLabelWidth"
          >
            <el-input class="inp1" v-model="ruleForm.road"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" size="mini"
            >取 消</el-button
          >
          <el-button type="primary" @click="addDigitalType()" size="mini"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 编辑弹出框 -->
      <el-dialog
        title="提示"
        :visible.sync="changeDialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <span class="inputName">(类型名称)</span>
        <el-form
          :model="ruleForm"
          :hide-required-asterisk="true"
          :status-icon="true"
          :rules="rules"
          ref="ruleForm"
        >
          <el-form-item label="品牌Id" :label-width="formLabelWidth">
            <el-input
              class="inp1"
              v-model="ruleForm.id"
              :disabled="true"
            ></el-input>
          </el-form-item>

          <el-form-item
            label="品牌名称"
            prop="changeName"
            :label-width="formLabelWidth"
          >
            <el-input class="inp1" v-model="ruleForm.changeName"></el-input>
          </el-form-item>
          <el-form-item
            label="图标路径"
            prop="changeRoad"
            :label-width="formLabelWidth"
          >
            <el-input class="inp1" v-model="ruleForm.changeRoad"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="changeDialogVisible = false" size="mini"
            >取 消</el-button
          >
          <el-button type="primary" @click="updateName()" size="mini"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 删除弹出框 -->
      <el-dialog
        title="提示"
        :visible.sync="delDialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <el-form
          :model="ruleForm"
          :hide-required-asterisk="true"
          :status-icon="true"
          :rules="rules"
          ref="ruleForm"
        >
          <p>确定删除此数码类别？</p>
          <el-form-item label="品牌id" :label-width="formLabelWidth">
            <el-input
              class="inp1"
              v-model="ruleForm.delId"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="品牌名称" :label-width="formLabelWidth">
            <el-input
              class="inp1"
              v-model="ruleForm.delName"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="图标路径" :label-width="formLabelWidth">
            <el-input
              class="inp1"
              v-model="ruleForm.delRoad"
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="delDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="delName()">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 搜索 -->
      <el-input
        class="el-input"
        placeholder="请输入要查询的用户"
        prefix-icon="el-icon-search"
        v-model="msg"
        clearable
      ></el-input>
      <!-- 添加 -->
      <el-button type="primary" class="bnt1" @click="dialogVisible = true"
        >添加数码类别</el-button
      >
      <!-- 表格 -->
      <el-table :data="search" style="width: 100%" border>
        <!-- 表头 -->
        <el-table-column
          prop="digital_brand_id"
          label="数码品牌Id"
          width="180"
          align="center"
          header-align="center"
        ></el-table-column>
        <el-table-column
          prop="digital_brand_name"
          label="数码品牌名称"
          width="180"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="digital_brand_image_road"
          label="品牌图标"
          width="180"
          header-align="center"
          align="center"
        ></el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" header-align="center" align="center">
          <template slot-scope="scope">
            <el-button
              type="success"
              plain
              size="mini"
              @click="changeDigitalCategory(scope.$index, scope.row)"
              >修改</el-button
            >
            <el-button
              type="danger"
              plain
              size="mini"
              @click="delDigitalCategory(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
import axios from 'axios';
import util from '../lib/index.js';
export default {
  data() {
    var validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入名称'));
      }
      else {
        callback();
      }
    };
    var validateRoad = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入品牌路径'));
      }
      else {
        callback();
      }
    };
    var validateChangeRoad = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入品牌路径'));
      }
      else {
        callback();
      }
    };
    var validateChangeName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('不能为空名称'));
      }
      else {
        callback();
      }
    };
    return {
      list: [],
      msg: '',
      dialogVisible: false,
      changeDialogVisible: false,
      delDialogVisible: false,
      text: '',
      formLabelWidth: '120px',
      ruleForm: {
        name: '',
        road: '',
        changeName: '',
        id: '',
        changeRoad: '',
        delId: '',
        delName: '',
      },
      rules: {
        name: [{ validator: validateName, trigger: 'blur' }],
        road: [{ validator: validateRoad, trigger: 'blur' }],
        changeRoad: [{ validator: validateChangeRoad, trigger: 'blur' }],
        changeName: [{ validator: validateChangeName, trigger: 'blur' }],
        id: [{ trigger: 'blur' }],
      },
    };
  },
  mounted() {
    util
      .allBrand({
        pages: 1,
      })
      .then((res) => {
        this.list = res.data.data;
      });
  },
  computed: {
    //搜索
    search() {
      let arr = [];
      for (let i = 0; i < this.list.length; i++) {
        if (
          (this.list[i].digital_brand_id + '').indexOf(this.msg) !== -1 ||
          this.list[i].digital_brand_image_road.indexOf(this.msg) !== -1 ||
          this.list[i].digital_brand_name.indexOf(this.msg) !== -1
        ) {
          arr.push({
            digital_brand_id: this.list[i].digital_brand_id,
            digital_brand_image_road: this.list[i].digital_brand_image_road,
            digital_brand_name: this.list[i].digital_brand_name,
          });
        } else if (this.msg === '') {
          arr = this.list;
        }
      }
      return arr;
    },
  },
  methods: {
    //   添加数码品牌类别
    addDigitalType() {
      this.$refs.ruleForm.validate((valid) => {
        console.log(this.ruleForm.name);
        if (valid) {
          util
            .brandAdd({
              digital_brand_name: this.ruleForm.name + '',
              digital_brand_image_road: this.ruleForm.road,
            })
            .tnen((r) => {
              util.allBrand({}).then((res) => {
                this.list = res.data.data;
              });
            });
          this.dialogVisible = false;
          this.ruleForm.name = '';
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 编辑弹出框
    changeDigitalCategory(item, row) {
      console.log('item', item);
      console.log('row', row);
      this.ruleForm.id = row.digital_brand_id;
      this.ruleForm.changeName = row.digital_brand_name;
      this.ruleForm.changeRoad = row.digital_brand_image_road;
      this.changeDialogVisible = true;
    },
    //编辑
    updateName() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          util
            .brandChangeInfor({
              digital_brand_id: this.ruleForm.id,
              digital_brand_name: this.ruleForm.changeName,
              digital_brand_image_road: this.ruleForm.changeRoad,
            })
            .then((res) => {
              util.allBrand({}).then((res) => {
                this.list = res.data.data;
              });
            });
          this.changeDialogVisible = false;
          this.ruleForm.changeName = '';
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      console.log(this.ruleForm.changeName);
      console.log(this.ruleForm.id);
    },
    //删除弹出框
    delDigitalCategory(item, row) {
      this.delDialogVisible = true;
      this.ruleForm.delId = row.digital_brand_id;
      this.ruleForm.delName = row.digital_brand_name;
      this.ruleForm.delRoad = row.digital_brand_image_road;
      console.log(row.digital_category_name);
      console.log(item);
    },
    //删除
    delName() {
      util
        .brandDelInfor({
          digital_brand_id: this.ruleForm.delId,
        })
        .then((res) => {
          util.allBrand({}).then((res) => {
            this.list = res.data.data;
          });
        });
      this.delDialogVisible = false;
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
  },
};
</script>
<style scoped>
.el-input {
    width: 400px;
    margin-bottom: 50px;
  }
  .el-card {
    margin-left: 86px;
    margin-top: 20px;
    padding: 10px 20px 10px;
    width: 800px;
  }
  .bnt1 {
    margin-left: 98px;
     margin-bottom: 50px;
  }
  .addDigitalTypeName {
    width: 200px;
    margin-left: 40px;
  }
  .inputName {
    position: absolute;
    left: 63px;
    top: 25px;
  }
</style>